<template>
  <div class="transport-create">
    <!-- 面包屑导航 -->
    <div class="breadcrumb">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>首页</el-breadcrumb-item>
        <el-breadcrumb-item>运输需求</el-breadcrumb-item>
        <el-breadcrumb-item>创建需求</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <!-- 操作按钮组 -->
    <el-card class="operation-card">
      <el-button-group>
        <el-button type="primary" icon="el-icon-scissors" @click="handleQuantitySplit">数量拆分</el-button>
      </el-button-group>
    </el-card>

    <!-- 查询条件 -->
    <el-card class="search-card">
      <div class="search-title">查询条件</div>
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="订单号：">
          <el-input v-model="searchForm.orderNo" placeholder="请输入订单号"></el-input>
        </el-form-item>
        <el-form-item label="客户名称：">
          <el-input v-model="searchForm.customerName" placeholder="请输入客户名称">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="出发城市：">
          <el-input v-model="searchForm.fromCity" placeholder="请输入出发城市">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="目的城市：">
          <el-input v-model="searchForm.toCity" placeholder="请输入目的城市">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查 询</el-button>
          <el-button @click="handleReset">重 置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 订单列表 -->
    <el-card class="table-card">
      <el-tabs v-model="activeTab" type="card">
        <el-tab-pane label="未提交" name="unsubmitted"></el-tab-pane>
        <el-tab-pane label="已提交" name="submitted"></el-tab-pane>
        <el-tab-pane label="已取消" name="cancelled"></el-tab-pane>
      </el-tabs>

      <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="orderNo" label="订单编号"></el-table-column>
        <el-table-column prop="orderType" label="订单类型"></el-table-column>
        <el-table-column prop="transportType" label="运输类型"></el-table-column>
        <el-table-column prop="customerName" label="客户名称"></el-table-column>
        <el-table-column prop="orderTime" label="下单时间"></el-table-column>
        <el-table-column prop="fromCity" label="出发城市"></el-table-column>
        <el-table-column prop="toCity" label="目的城市"></el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="handleView(scope.row)">查看</el-button>
            <el-button type="text" size="small" @click="handleEdit(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加数量拆分弹窗 -->
    <el-dialog title="按数量拆分需求单据" :visible.sync="splitDialogVisible" width="80%">
      <div class="split-content">
        <!-- 基本信息 -->
        <el-form :model="splitForm" label-width="100px" size="small">
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="需求单号">
                <el-input v-model="splitForm.requestNo"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="订单编号">
                <el-input v-model="splitForm.orderNo"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="订单类型">
                <el-select v-model="splitForm.orderType" placeholder="请选择">
                  <el-option label="运输订单" value="transport"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="运输方式">
                <el-select v-model="splitForm.transportType" placeholder="请选择">
                  <el-option label="公路整车" value="truck"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="接单日期">
                <el-date-picker v-model="splitForm.orderDate" type="date" placeholder="选择日期"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="路线">
                <el-input v-model="splitForm.route"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="要求到货日期">
                <el-date-picker v-model="splitForm.deliveryDate" type="date" placeholder="选择日期"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="是否急单">
                <el-checkbox v-model="splitForm.isUrgent"></el-checkbox>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <!-- 货物列表 -->
        <div class="goods-list">
          <div class="section-title">需求单号：{{splitForm.requestNo}}</div>
          <el-table :data="splitForm.goodsList" border>
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="name" label="货物名称"></el-table-column>
            <el-table-column prop="remainQuantity" label="剩余数量"></el-table-column>
            <el-table-column prop="splitQuantity" label="拆分数量">
              <template slot-scope="scope">
                <el-input v-model="scope.row.splitQuantity" size="mini"></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="weight" label="单件重量(千克)"></el-table-column>
            <el-table-column prop="length" label="长(米)"></el-table-column>
            <el-table-column prop="width" label="宽"></el-table-column>
            <el-table-column prop="height" label="高"></el-table-column>
            <el-table-column prop="volume" label="单件体积"></el-table-column>
            <el-table-column prop="package" label="包装"></el-table-column>
            <el-table-column prop="condition" label="运输条件"></el-table-column>
          </el-table>
          <div class="table-actions">
            <el-button type="primary" size="small" @click="handleGenerateRequest">生成新需求单</el-button>
            <el-button type="danger" size="small" @click="handleDeleteRequest">删除该需求单</el-button>
          </div>
        </div>

        <!-- 生成的需求单列表 -->
        <div v-for="(request, index) in generatedRequests" :key="index" class="request-item">
          <div class="section-title">需求单号：{{splitForm.requestNo}}</div>
          <el-table :data="[request]" border>
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="name" label="货物名称"></el-table-column>
            <el-table-column prop="remainQuantity" label="剩余数量"></el-table-column>
            <el-table-column prop="splitQuantity" label="拆分数量"></el-table-column>
            <el-table-column prop="weight" label="单件重量(千克)"></el-table-column>
            <el-table-column prop="length" label="长(米)"></el-table-column>
            <el-table-column prop="width" label="宽"></el-table-column>
            <el-table-column prop="height" label="高"></el-table-column>
            <el-table-column prop="volume" label="单件体积"></el-table-column>
            <el-table-column prop="package" label="包装"></el-table-column>
            <el-table-column prop="condition" label="运输条件"></el-table-column>
          </el-table>
          <div class="table-actions">
            <el-button type="primary" size="small" @click="handleGenerateRequest">生成新需求单</el-button>
            <el-button type="danger" size="small" @click="handleDeleteRequest">删除该需求单</el-button>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="splitDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirmSplit">确 认</el-button>
      </div>
    </el-dialog>

    <!-- 在页面最底部添加提交按钮 -->
    <div class="bottom-actions">
      <el-button type="success" icon="el-icon-check" @click="handleSubmit">提交</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TransportCreate',
  data() {
    return {
      searchForm: {
        orderNo: '',
        customerName: '', 
        fromCity: '',
        toCity: ''
      },
      activeTab: 'unsubmitted',
      tableData: [
        {
          orderNo: '0990900',
          orderType: '',
          transportType: '',
          customerName: '上海亚源',
          orderTime: '2015-07-30 20:24:10',
          fromCity: '上海',
          toCity: '北京'
        }
      ],
      splitDialogVisible: false,
      splitForm: {
        requestNo: '',
        orderNo: '',
        orderType: '',
        transportType: '',
        orderDate: '',
        route: '',
        deliveryDate: '',
        isUrgent: false,
        goodsList: []
      },
      generatedRequests: []
    }
  },
  methods: {
    handleSearch() {
      console.log('搜索条件：', this.searchForm)
    },
    handleReset() {
      this.searchForm = {
        orderNo: '',
        customerName: '',
        fromCity: '',
        toCity: ''
      }
    },
    handleSelectionChange(selection) {
      console.log('选中的行：', selection)
    },
    handleView(row) {
      console.log('查看行：', row)
    },
    handleEdit(row) {
      console.log('编辑行：', row)
    },
    handleQuantitySplit() {
      this.splitDialogVisible = true
    },
    handleSubmit() {
      console.log('提交')
    },
    handleGenerateRequest() {
      console.log('生成新需求单')
    },
    handleDeleteRequest() {
      console.log('删除该需求单')
    },
    handleConfirmSplit() {
      console.log('确认拆分')
    }
  }
}
</script>

<style scoped>
.transport-create {
  padding: 20px;
}

.breadcrumb {
  margin-bottom: 20px;
}

.search-card {
  margin-bottom: 20px;
}

.search-title {
  font-size: 14px;
  color: #606266;
  margin-bottom: 20px;
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.table-card {
  margin-bottom: 20px;
}

.el-tabs {
  margin-bottom: 15px;
}

:deep(.el-card__body) {
  padding: 20px;
}

:deep(.el-form--inline .el-form-item) {
  margin-right: 0;
  margin-bottom: 10px;
}

.operation-card {
  margin-bottom: 20px;
}

.operation-card .el-button-group {
  margin-bottom: 0;  /* 覆盖原来的下边距 */
}

.split-content {
  padding: 20px;
}

.section-title {
  font-size: 14px;
  color: #606266;
  margin-bottom: 20px;
}

.goods-list {
  margin-bottom: 20px;
}

.generated-list {
  margin-bottom: 20px;
}

.table-actions {
  margin-top: 20px;
  text-align: right;
}

.dialog-footer {
  text-align: right;
}

.bottom-actions {
  margin-top: 20px;
  text-align: right;
}
</style> 